"""
分配模型
"""
from sqlalchemy import Column, Integer, Enum, ForeignKey, DateTime, func
from sqlalchemy.orm import relationship
from .base import Base


class StudentCourseAssignment(Base):
    """学员课程关系表（班主任管理）"""
    __tablename__ = "student_course_assignments"
    
    student_id = Column(Integer, ForeignKey("students.id"), nullable=False, comment="学员ID")
    course_id = Column(Integer, ForeignKey("courses.id"), nullable=False, comment="课程ID")
    order_id = Column(Integer, ForeignKey("orders.id"), nullable=False, comment="订单ID")
    class_teacher_id = Column(Integer, ForeignKey("employees.id"), nullable=False, comment="班主任ID")
    enrollment_date = Column(DateTime, nullable=False, comment="入学日期")
    contract_status = Column(Enum("pending", "signed", "expired", name="contract_status"), 
                            default="pending", comment="合同状态")
    status = Column(Enum("active", "completed", "suspended", "dropped", name="assignment_status"), 
                   default="active", comment="学习状态")
    
    # 关系
    student = relationship("Student", back_populates="course_assignments")
    course = relationship("Course", back_populates="student_assignments")
    order = relationship("Order", back_populates="course_assignments")
    class_teacher = relationship("Employee", back_populates="class_assignments")
    
    def __repr__(self):
        return f"<StudentCourseAssignment(id={self.id}, student_id={self.student_id}, course_id={self.course_id})>"


class TeacherStageAssignment(Base):
    """讲师课程阶段分配表"""
    __tablename__ = "teacher_stage_assignments"
    
    teacher_id = Column(Integer, ForeignKey("employees.id"), nullable=False, comment="讲师ID")
    course_id = Column(Integer, ForeignKey("courses.id"), nullable=False, comment="课程ID")
    stage_number = Column(Integer, nullable=False, comment="阶段序号")
    status = Column(Enum("active", "inactive", name="assignment_status"), 
                   default="active", comment="状态")
    
    # 关系
    teacher = relationship("Employee", back_populates="teacher_assignments")
    course = relationship("Course", back_populates="teacher_assignments")
    
    def __repr__(self):
        return f"<TeacherStageAssignment(id={self.id}, teacher_id={self.teacher_id}, course_id={self.course_id})>"
